﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;

namespace PlaylistParitySL
{
    public static class ITunesUtilities
    {
        public static ObservableCollection<Playlist> GetPlaylists()
        {
            ObservableCollection<Playlist> returnList = new ObservableCollection<Playlist>();

            if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) + @"\iTunes\iTunes Music Library.xml"))
            {
                return returnList;
            }

            StreamReader reader = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) + @"\iTunes\iTunes Music Library.xml");
            
            var doc = XDocument.Load(reader);

            var nodes = from node in doc.Root.Descendants("array")
                        select node;

            XElement node1 = nodes.First();
            foreach (XElement library in node1.Nodes())
            {

                Playlist p = new Playlist();
                var nameNode = library.Descendants("string");

                if (nameNode.First().Value != "Library")
                {

                    p.Name = nameNode.First().Value.Trim();

                    var songArray = library.Descendants("dict");
                    foreach (var song in songArray)
                    {
                        var s = song.Descendants("integer");
                        var s1 = s.First();
                        int i = int.Parse(s1.Value.Trim());
                        Song songObject = PopulateSongInfo(i, doc);
                        p.SongList.Add(songObject);
                    }

                    returnList.Add(p);
                }

            }

            return returnList;
        }

        private static Song PopulateSongInfo(int i, XDocument doc)
        {
            Song returnSong = new Song();
            returnSong.TrackID = i;

            var nodes = from node in doc.Root.Descendants("key")
                        where node.Value.Equals("Tracks")
                        select node.NextNode;

            string songNum = i.ToString();

            XElement tracksElement = nodes.First() as XElement;
            var nodes2 = from node in tracksElement.Descendants("key")
                         where node.Value == songNum
                         select node.NextNode;

            XElement songElement = nodes2.First() as XElement;

            returnSong.Name = GetInfoFromElement("Name", songElement);

            returnSong.Artist = GetInfoFromElement("Artist", songElement);

            returnSong.Album = GetInfoFromElement("Album", songElement);

            if (returnSong.Album == string.Empty)
            {
                returnSong.Album = GetInfoFromElement("Album Artist", songElement);
            }

            returnSong.Location = GetInfoFromElement("Location", songElement);

            //locationString = locationString.Replace(@"file://localhost/", "");

            return returnSong;
        }

        private static string GetInfoFromElement(string p, XElement songElement)
        {
            var name = from node in songElement.Descendants("key")
                       where node.Value == p
                       select node.NextNode;

            if (name.Count() > 0)
            {
                XElement nameElement = name.First() as XElement;
                return nameElement.Value;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
